// 引入数据库
const {
  mysqlFun
} = require('../mysql/mysql');
// 番剧页面数据
exports.fanopera = function (app) {
  // 获取番剧
  app.post('/getFanopera', (req, res) => {
    const sql = `select * from fanlist`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'F0001',
          msg: '获取番剧列表成功',
          list: data
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 写入番剧
  app.post('/KingUpdataFan', (req, res) => {
    const sql = `INSERT INTO fanlist(fanName,fanUrl,fanList) value ('${req.body.fanName}','${req.body.fanUrl}','${req.body.fanList}')`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'A0001',
          msg: '写入番剧成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  // 删除番剧
  app.post('/deleteFanId', (req, res) => {
    const sql = `delete from fanlist where fanid=${req.body.id}`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'D0001',
          msg: '删除成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })

  //更新番剧内容
  app.post('/KingUpdataFanlist', (req, res) => {
    const sql = `update fanlist set fanName='${req.body.fanName}',fanUrl='${req.body.fanUrl}',fanList='${req.body.fanList}' where fanid=${req.body.id}`;

    function obj(data) {
      if (data === undefined) {
        return;
      }
      if (data.length !== 0) {
        res.json({
          code: 'U0001',
          msg: '番剧更新成功'
        })
      }
    }
    mysqlFun(sql, obj);
  })
}